/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.pharmproject.dao;

import br.com.pharmproject.bean.Categoria;
import br.com.pharmproject.conexao.Conexao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

/**
 *
 * @author rodrigo
 */
public class CatergoriaDAO {
    
    
    private List<Categoria> categorias;
    
    private Categoria categoria;
        
    Statement stm = null;
    
    
    public CatergoriaDAO() throws ClassNotFoundException,  SQLException{
        Conexao.getInstance();
        stm = Conexao.getInstance().getConector().createStatement();
    }
    
    
    public Categoria getById(int idCategoria) throws SQLException, 
            ClassNotFoundException{
         categoria = new Categoria();
         
         String sql = "SELECT * FROM CATEGORIA_PRODUTO cat WHERE "
                 + "cat.ID_CATEGORIA = ?";
         
         PreparedStatement pstm = Conexao.getInstance().getConector()
                 .prepareStatement(sql);
         
         ResultSet rs = pstm.executeQuery();
         
         while(rs.next()){
             
             categoria.setNoCategoria(rs.getString("NOME"));
             categoria.setDsCategoria(rs.getString("DESCRICAO"));
                         
         }
        
                 
         return categoria;
    }
    
    public void insert(Categoria categoria) throws ClassNotFoundException, 
            SQLException{
        
        
        String sqlUpdate = "INSERT INTO CATEGORIA_PRODUTO "
              + "(NOME, DESCRICAO)" + " VALUES "
              + "(?, ?)";
         
        PreparedStatement pstm = Conexao.getInstance()
                 .getConector().prepareStatement(sqlUpdate);
        pstm.setString(1, categoria.getNoCategoria());
        pstm.setString(2, categoria.getDsCategoria());

        pstm.executeUpdate(sqlUpdate);
        
    }
    
    
    public void update(Categoria categoria) throws SQLException, ClassNotFoundException{
        
        
        String sqlUpdate = "UPDATE CATEGORIA_PRODUTO set NOME = ?, "
                + "DESCRICAO = ? WHERE ID_CATEGORIA = ?";
        
        
        PreparedStatement pstm = Conexao.getInstance().getConector()
                .prepareStatement(sqlUpdate);
        
        pstm.setString(1, categoria.getNoCategoria());
        pstm.setString(2, categoria.getDsCategoria());
        
        pstm.executeUpdate();
        
               
    }
    
    public void delete(int idCategoria){
        
        String sqlDelete = "DELETE FROM CATEGORIA_PRODUTO "
                + "WHERE ID_CATEGORIA_PRODUTO = ? ";
        
        PreparedStatement pstm = null;
        ResultSet rs = null;
                
        try {
            
            pstm = Conexao.getInstance().getConector()
                    .prepareStatement(sqlDelete);
            
            pstm.setInt(1, idCategoria);
            pstm.executeUpdate();
            
            
            
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            Conexao.getInstance().closeConnection();
        }
        
    }
    
    
}
